﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

namespace VkSample63
{
    public class TransformSystem : ComponentSystem
    {
        private Camera Camera;
        private float AngleX;
        private float AngleY;
        private int samplerType = 0;
        private int texType = 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public TransformSystem()
        {
        }

        public override void Initialization()
        {
            int m = 0;
            ContextManager.Foreach<Group>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                if (m != texType) context.State = -1;
                if(Camera == null)
                {
                    IRenderGroup renderGroup = context.GetContextRenderGroup();
                    Camera = renderGroup.Camera;
                }
                m++;
            });
        }

        public override void Update(TimeSpan timeSpan)
        {
            float diffX = 0;
            float diffY = 0;
            bool isButtonUp = false;
            bool isLeft = false;
            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                Vector2 p = Input.GetMousePositionDelta();
                diffX = p.X;
                diffY = p.Y;
            }
            if(Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Click)
            {
                isButtonUp = true;
                var position = Input.GetMousePosition();
                if(position.X < Camera.Viewport.Width / 2)
                {
                    samplerType++;
                    samplerType %= 4;
                    isLeft = true;
                }
                else
                {
                    texType++;
                    texType %= 3;
                }
            }
            AngleX += diffX * 180 / 200;
            AngleY += diffY * 180 / 200;
            int index = 0;
            ContextManager.Foreach<Group>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                ref MVPMatrix mvp = ref context.GetMVPMatrix();
                mvp.ModelMatrix = Matrix4x4.CreateRotationY(AngleX / 180.0f * MathF.PI) *
                Matrix4x4.CreateRotationZ(AngleY / 180.0f * MathF.PI);
                if (isButtonUp)
                {
                    if (isLeft)
                    {
                        context.SetCustomPropery("Index", samplerType);
                    }
                    else
                    {
                        if (index != texType) context.State = -1;
                        else context.State = 0;
                    }
                }
                index++;
                context.PushToRenderGroup();
            });
        }

        public struct Group
        {
            public Entity Entity;
            public TransformComponent Transform;
        }
    }
}
